package com.main.tripplanner.servlet;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.http.MediaType;

import com.main.tripplanner.clientservices.QuestionClientService;
import com.main.tripplanner.persistent.Attraction;
import com.main.tripplanner.persistent.Ticket;
import com.main.tripplanner.persistent.Vote;

public class QuestionServlet extends BaseServlet{
	private QuestionClientService service;
	
	@Override
	protected void initialize() {
		try {
			service = getService(QuestionClientService.class);
			if (service == null)
				service = QuestionClientService.getService();
		} catch (Exception e) {
//			BaseService.log(this, "Initialization failed.", e);
		}
		
	}

	@Override
	protected void get(HttpServletRequest request,
			HttpServletResponse response, Ticket ticket) throws IOException {
		String countryId = getParameter(request, "countryId");
		String serviceResponse = service.getResponse(countryId);
		ticket.setResponse(serviceResponse);
		putBody(response, ticket, MediaType.APPLICATION_JSON_VALUE);
	}

	@Override
	protected void post(HttpServletRequest request,
			HttpServletResponse response, Ticket ticket) throws IOException {
		Vote vote = deserialize(ticket.getRequest(), Vote.class);
		Attraction attraction = service.getResponse(vote); 
		ticket.setResponse(attraction.toJsonString(true));
		putBody(response, ticket, MediaType.APPLICATION_JSON_VALUE);
	}

	@Override
	protected void delete(HttpServletRequest request,
			HttpServletResponse response, Ticket ticket) throws IOException {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void put(HttpServletRequest request,
			HttpServletResponse response, Ticket ticket) throws IOException {
		// TODO Auto-generated method stub
		
	}

}
